package com.wwwarehouse.usercenter.fragment.business_relationship;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.adapter.BRCustomerFileAdapter;
import com.wwwarehouse.usercenter.adapter.SelectPermissionPagerAdapter;
import com.wwwarehouse.usercenter.bean.business_relationship.BRCustomerFileListBean;
import com.wwwarehouse.usercenter.bean.permission_recycle.AuthorizedOutItensBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;
import com.wwwarehouse.usercenter.fragment.permission_recycle.PRSelectPagerFragment;
import com.wwwarehouse.usercenter.fragment.permission_recycle.PRUserCardAuthorityAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by xiaoke.ni on 2017/10/11.
 */

public class BRCustomerFileListFragment extends BaseFragment {

    private RecyclerView mRecyclerView;
    private List<BRCustomerFileListBean.BusinessListBean> mDataList;
    private BRCustomerFileAdapter mRvAdapter;

    private String mBusinessUnitId;
    private int page;
    private String orderSort;
    private List<String> salesManIds;
    private List<String> tagIds;
    private String searchText;
    private String relationType;

    public interface OnButtonClickedListener {
        void onButtonClicked();
    }

    private OnButtonClickedListener mButtonClickedListener;

    public void setOnButtonClickedListener(OnButtonClickedListener onButtonClickedListener) {
        mButtonClickedListener = onButtonClickedListener;
    }

    public static BRCustomerFileListFragment newInstance(int page, String mBusinessUnitId, String orderSort, ArrayList<String> tagIds, ArrayList<String> salesManIds, String searchText, ArrayList<BRCustomerFileListBean.BusinessListBean> mDataList,String relationType) {
        BRCustomerFileListFragment fragment = new BRCustomerFileListFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(UserCenterConstant.KEY_BR_CUSTOMER_FILE_ITEM_BEAN, mDataList);
        bundle.putString("businessId", mBusinessUnitId);
        bundle.putString("orderSort", orderSort);
        bundle.putInt("page", page);
        bundle.putStringArrayList("tagIds", tagIds);
        bundle.putStringArrayList("salesManIds", salesManIds);
        bundle.putString("searchText", searchText);
        bundle.putString("relationType",relationType);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mDataList = bundle.getParcelableArrayList(UserCenterConstant.KEY_BR_CUSTOMER_FILE_ITEM_BEAN);
            page = bundle.getInt("page");
            orderSort = bundle.getString("orderSort");
            mBusinessUnitId = bundle.getString("businessId");
            tagIds = bundle.getStringArrayList("tagIds");
            salesManIds = bundle.getStringArrayList("salesManIds");
            searchText = bundle.getString("searchText");
            relationType = bundle.getString("relationType");
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list_distribute_authority, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        if (page == 1) {
            initData();
        }else{
            getRelationBusinessUnitList();
        }
    }

    private void initData(){
        mRecyclerView = $(R.id.rv);
        mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mRvAdapter = new BRCustomerFileAdapter(mDataList, mRecyclerView.getHeight());
                mRvAdapter.setOnItemClickListener(new BRCustomerFileAdapter.OnItemClickListener() {
                    @Override
                    public void onButtonClicked(int position) {
                        BRCustomerFileListBean.BusinessListBean businessListBean = mDataList.get(position);
                        if (mButtonClickedListener != null) {
                            mButtonClickedListener.onButtonClicked();
                        }
                        BRCustomerFileDetailFragment fragment = new BRCustomerFileDetailFragment();
                        Bundle bundle = new Bundle();
                        bundle.putString("relationUkid",businessListBean.getRelationUkid() + "");
                        bundle.putString("buId",mBusinessUnitId);
                        fragment.setArguments(bundle);
                        pushFragment(fragment);
//                        mRvAdapter.notifyDataSetChanged();
                    }
                });
                mRecyclerView.setAdapter(mRvAdapter);
                mRecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    }

    /**
     * 客户档案列表
     */
    private void getRelationBusinessUnitList() {
        Map<String, Object> map = new HashMap<>();
        map.put("buId", mBusinessUnitId);
        map.put("orderSort", orderSort);
        if (!TextUtils.isEmpty(orderSort))
            map.put("orderType", "related_time");
        map.put("page", page);
        map.put("relationType", relationType);
        if (salesManIds != null && salesManIds.size() > 0)
            map.put("salesManIds", orderSort);
        if (!TextUtils.isEmpty(searchText))
            map.put("searchText", searchText);
        map.put("size", BRAddFriendsFragment.SIZE);
        if (tagIds != null && tagIds.size() > 0)
            map.put("tagIds", tagIds);
        NoHttpUtils.httpPost(UserCenterConstant.GET_RELATION_BUSINESS_UBIT_LIST, map, onResponseListener, 0);
    }

    private NoHttpUtils.OnResponseListener onResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case 0:
                    if (TextUtils.equals("0", commonClass.getCode())) {
                        mDataList = JSON.parseObject(commonClass.getData().toString(), BRCustomerFileListBean.class).getBusinessList();
                        initData();
                    } else {
                    }
                    break;
            }
        }

        @Override
        public void onFailed(String s, int i) {

        }

        @Override
        public void onFinish(int i) {
            dismissProgressDialog();
        }
    };

    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {

    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }
}
